package com.yidu.hbats.action.AtsBase;

import com.yidu.hbats.model.AtsBaseSport;
import com.yidu.hbats.model.AtsDataDic;
import com.yidu.hbats.service.atsBase.AtsDataDicService;
import com.yidu.springbootdemo.permission.tools.Pages;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @program: spring-boot-demo
 * @description: ${description}
 * @author: clg
 * @create: 2019-10-31 09:05
 */

@Controller
@RequestMapping("atsDataDicController")

public class AtsDataDicController {

    @Resource
    AtsDataDicService atsDataDicService;

    AtsDataDic atsDataDic;


    /**
     * 查询所有和分页
     * @param atsDataDic
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")

    public Map<String,Object> selectAll(AtsDataDic atsDataDic, Page page){

        Map map = new HashMap();
        List<AtsDataDic> list = atsDataDicService.selectAll(atsDataDic,page);

        int count = atsDataDicService.selectCount(atsDataDic);

        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        return map;

    }

    /**
     * 增加或者修改
     * @param atsDataDic
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    public Message saveOrUpdate(AtsDataDic atsDataDic,HttpServletRequest request){
        Message mg=new Message();

        int rows=0;
        if(atsDataDic.getDid()!=null){//修改
            rows=atsDataDicService.updateByPrimaryKeySelective(atsDataDic);
        }else{//增加
            Date date = new Date();
            atsDataDic.setCreattime(date);
            System.out.println(date);
            atsDataDic.setIsva(1);
            rows=atsDataDicService.insert(atsDataDic);
        }
        if(rows>0){
            mg.setNum(1);
            mg.setMsg("操作成功");
        }else{
            mg.setNum(0);
            mg.setMsg("操作失败");
        }
        return mg;
    }


    /**
     * 删除
     * @param atsDataDic
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteByPrimaryKey")

    public Message deleteByPrimaryKey (AtsDataDic atsDataDic){

        Message message=new Message();

        atsDataDic.setIsva(0);
        atsDataDicService.updateIsva(atsDataDic);
        message.setNum(1);
        message.setMsg("删除成功");
        return  message;
    }

    @ResponseBody
    @RequestMapping("selectByPrimaryKey")
    public AtsDataDic selectByPrimaryKey(Integer did){
        return atsDataDicService.selectByPrimaryKey(did);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(String ids){
        Message message = new Message();
        System.out.println("id"+ids);
        String id[] = ids.split(",");
        int rows=0;
        for (int i = 0;i<id.length;i++){
            AtsDataDic atsDataDic=new AtsDataDic();
            atsDataDic.setDid(Integer.valueOf(id[i]));
            atsDataDic.setIsva(0);

            atsDataDicService.updateIsva(atsDataDic);
            rows++;
        }
        message.setNum(1);
        message.setMsg("恭喜,删除成功!共删除"+rows+"行记录!");
        return message;
    }

    /**
     * 根据字典编码查询数据
     * @param atsDataDic
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public List<AtsDataDic> findAll(AtsDataDic atsDataDic){
        List<AtsDataDic> list=atsDataDicService.findAll(atsDataDic);

        return list;
    }

}

